//
//  GalleryTableViewCell.m
//  parent
//
//  Created by IreWesT on 15/6/1.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "GalleryTableViewCell.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "UIView+Additions.h"
#import "GalleryViewController.h"

@implementation GalleryTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _floorIndex = 0;
        [self _initView];
    }
    return self;
}

//初始化子视图
- (void)_initView {
    
    _headView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 36, 36)];
    _headView.layer.cornerRadius = 18.0f;
    _headView.layer.masksToBounds = YES;
    _headView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_headView];
    
    _nickname = [[UILabel alloc] initWithFrame:CGRectMake(_headView.right + 18, 16, 200, 14)];
    _nickname.backgroundColor = [UIColor clearColor];
    _nickname.textColor = UIColorMake(41, 89, 152);
    _nickname.font = [UIFont systemFontOfSize:14.0f];
    [self.contentView addSubview:_nickname];
    
    _date = [[UILabel alloc] initWithFrame:CGRectMake(_nickname.left, _nickname.bottom + 5.5, _nickname.width, 9)];
    _date.backgroundColor = [UIColor clearColor];
    _date.textColor = UIColorMake(112, 112, 112);
    _date.font = [UIFont systemFontOfSize:9.0f];
    [self.contentView addSubview:_date];
    
    _content = [[UILabel alloc] initWithFrame:CGRectMake(16, _headView.bottom + 16, APP_SCREEN_WIDTH - 16 * 2, 14)];
    _content.backgroundColor = [UIColor clearColor];
    _content.textColor = UIColorMake(45, 45, 45);
    _content.font = [UIFont systemFontOfSize:14.0f];
    _content.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:_content];
    
    _picView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _content.bottom + 12, APP_SCREEN_WIDTH, 10)];
    _picView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_picView];
    
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, _picView.bottom, APP_SCREEN_WIDTH, 46)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bottomView];
    
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(16, (_bottomView.height - 12) / 2, 12, 12)];
    icon.backgroundColor = [UIColor clearColor];
    icon.image = [UIImage imageNamed:@"gallery_icon_love"];
    [_bottomView addSubview:icon];
    
    _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(icon.right + 9, icon.top, 60, 12)];
    _countLabel.backgroundColor = [UIColor clearColor];
    _countLabel.textColor = UIColorMake(113, 112, 112);
    _countLabel.font = [UIFont systemFontOfSize:12.0f];
    [_bottomView addSubview:_countLabel];
    
    UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(APP_SCREEN_WIDTH - 85, (_bottomView.height - 18) / 2, 18, 18)];
    shareButton.backgroundColor = [UIColor clearColor];
    shareButton.tag = _floorIndex;
    [shareButton setImage:[UIImage imageNamed:@"gallery_btn_share"] forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(clickShareButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:shareButton];
    _shareButton = shareButton;
    _shareButton.hidden = YES;
    
    _loveButton = [[UIButton alloc] initWithFrame:CGRectMake(shareButton.right + 30, shareButton.top-7, 25, 25)];
    _loveButton.backgroundColor = [UIColor clearColor];
    _loveButton.tag = _floorIndex;
    [_loveButton setImage:[UIImage imageNamed:@"gallery_btn_noloved"] forState:UIControlStateNormal];
    [_loveButton addTarget:self action:@selector(clickLoveButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_loveButton];
}
/*
- (void)showImage:(UITapGestureRecognizer *)tapReconizer
{
    UIImageView *picView = (UIImageView *)tapReconizer.view;
    
    originFrame = [picView convertRect:picView.bounds toView:nil];
    UIViewController *viewController = [[UIViewController alloc]init];
    viewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    viewController.view.frame = CGRectMake(0, 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT);
    viewController.view.backgroundColor = RGBACOLOR(33, 33, 33, 0.5);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage)];
    viewController.view.userInteractionEnabled = YES;
    [viewController.view addGestureRecognizer:tap];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:originFrame];
    imageView.tag = 1;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //imageView.image = picView.image;
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _avatar_header,_data.replyUserPic]]
                 placeholderImage:picView.image];
    [viewController.view addSubview:imageView];
    _showImageView = imageView;
    [[self viewController] presentViewController:viewController
                                        animated:NO
                                      completion:^(void){
                                          
                                          CGSize size = picView.image.size;
                                          CGSize fullSize = CGSizeMake(APP_SCREEN_WIDTH - 20, APP_SCREEN_HEIGHT - 40);
                                          float newWidth = size.width;
                                          float newHeight = size.height;
                                          float scale = size.width / size.height;
                                          if (scale >= (fullSize.width / fullSize.height)) {
                                              if (size.width > fullSize.width) {
                                                  newWidth = fullSize.width;
                                                  newHeight = newWidth / scale;
                                              }
                                          }else {
                                              if (size.height > fullSize.height) {
                                                  newHeight = fullSize.height;
                                                  newWidth = newHeight * scale;
                                              }
                                          }
                                          [UIView animateWithDuration:.3f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
                                              imageView.frame = CGRectMake((APP_SCREEN_WIDTH - newWidth) / 2, (APP_SCREEN_HEIGHT - newHeight) / 2, newWidth, newHeight);} completion:nil];
                                      }];
    
}
 */
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    NSDictionary *user = _data[@"uinfo"];
    NSString *headUrl = [NSString stringWithFormat:@"%@%@", _picPrefix, user[@"avatar"]];
    [_headView sd_setImageWithURL:[NSURL URLWithString:headUrl]];
    _nickname.text = [NSString stringWithFormat:@"%@", user[@"name"]];
    _date.text = [NSString stringWithFormat:@"%@", _data[@"time"]];
    _content.text = [NSString stringWithFormat:@"%@", _data[@"title"]];
    NSString *picUrl = [NSString stringWithFormat:@"%@%@", _picPrefix, _data[@"srcurl"]];
    [_picView sd_setImageWithURL:[NSURL URLWithString:picUrl]];
    _countLabel.text = [NSString stringWithFormat:@"%@", _data[@"praise"]];
    
    CGFloat width = [_data[@"width"] floatValue];
    CGFloat heigth = [_data[@"height"] floatValue];
    CGFloat scale = heigth / width;
    
    _picView.height = scale * _picView.width;
    _bottomView.top = _picView.bottom;
    
    if ([_data[@"isloved"] integerValue] == 1) {
        [_loveButton setImage:[UIImage imageNamed:@"gallery_btn_loved"] forState:UIControlStateNormal];
    }else {
        [_loveButton setImage:[UIImage imageNamed:@"gallery_btn_noloved"] forState:UIControlStateNormal];
    }
    
    _shareButton.tag = _floorIndex;
    _loveButton.tag = _floorIndex;
}

- (void)clickShareButton:(UIButton *)button {

    GalleryViewController *gallery = (GalleryViewController *)[self viewController];
    [gallery clickShareButton:button];
}

- (void)clickLoveButton:(UIButton *)button {
    
    GalleryViewController *gallery = (GalleryViewController *)[self viewController];
    [gallery clickLoveButton:button];
}

@end
